java convert date to localdate

122

string to localdate in java -

  DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/MM/yyyy");
  String date = "16/08/2016";

  //convert String to LocalDate
  LocalDate localDate = LocalDate.parse(date, formatter);

java convert java.util.Date to LocalDate -

Date yourDate = new Date(); // this will be your java.util.Date instance to convert
LocalDate date = yourDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

how to turna date into a LocalDateTime java -

public LocalDateTime convertToLocalDateTimeViaMilisecond(Date dateToConvert) {
    return Instant.ofEpochMilli(dateToConvert.getTime())
      .atZone(ZoneId.systemDefault())
      .toLocalDateTime();
}

local date to date java -

public LocalDate convertToLocalDateViaInstant(Date dateToConvert) {
    return dateToConvert.toInstant()
      .atZone(ZoneId.systemDefault())
      .toLocalDate();
}

Comments

Submit
0 Comments